import {
	getsildeData,
	getlimitData,
	getmoreData,
	getcategorylistData,
	getcategoryData,
	getcategorylist,
	getcategoryrightData,
	getdetail,
	getLogin,
	getPhoneLoginApi
} from "@/api/index"
const actions = {
	//首页轮播数据
	async ActchangesildeData(context, params) {
		context.commit("changeshowloading", false)
		let result = await getsildeData(params)
		if (result.status == "success") {
			context.commit("changesildeData", JSON.parse(result.data.data))
			context.commit("changeshowloading", true)
		}
	},
	//首页秒杀数据
	async ActchangelimitData(context, params) {
		let result = await getlimitData(params)
		context.commit("changelimitData", result.data)
	},
	//首页更多商品列表数据
	async ActchangemoreData(context, params) {
		context.commit("changeloading", true)
		let result = await getmoreData(params)
		if (result.status == "success") {
			context.commit("changemoreData", result.data)
			context.commit("changeloading", false)
		}
		if (result.data.length < 10) {
			context.commit("changehideload", true)
		}
	},
	//首页分类标题数据
	async ActchangecategorylistData(context, params) {
		let result = await getcategorylistData(params)
		context.commit("changecategorylistData", result.data)
	},
	//首页分类上方内容数据
	async ActchangecategoryData(context, params) {
		let result = await getcategoryData(params)
		context.commit("changecategoryData", result.data)
	},
	//首页分类下方列表数据
	async Actchangecategorylist(context, params) {
		context.commit("changeloading", true)
		let result = await getcategorylist(params)
		if (result.status == "success") {
			context.commit("changecategorylist", result.data)
			context.commit("changeloading", false)
		}
		if (result.data.length < 10) {
			context.commit("changehideload", true)
		}
	},
	//分类右方数据
	async ActchangecategoryrightData(context, cat_id) {
		context.commit("changeshowloading", false)
		let resultleft = await getcategorylistData()
		let result = await getcategoryrightData(cat_id)
		var obj = {}
		for (var i = 0; i < resultleft.data.length; i++) {
			if (resultleft.data[i].cat_id == cat_id) {
				obj = resultleft.data[i]
			}
		}
		context.commit("changeadData", obj)
		if (result.status == "success") {
			context.commit("changeshowloading", true)
			context.commit("changecategoryrightData", result.data)
			context.commit("changeId", cat_id)
		}
	},
	//分类列表详情数据
	async Actcategorydetailist(context, params) {
		context.commit("changeloading", true)
		let result = await getcategorylist(params)
		if (result.status == "success") {
			context.commit("changecategorydetailist", result.data)
			context.commit("changeloading", false)
		}
		if (result.data.length < 10) {
			context.commit("changehideload", true)
		}
	},
	//详情页数据
	async ActchangedetailData(context, params) {
		context.commit("changeshowloading", false)
		let result = await getdetail(params)
		if (result.status == "success") {
			context.commit("changeshowloading", true)
			context.commit("changedetailData", result.data)
		}
	},
	// 账号登陆
	async ActChangeIsLogin(context, params) { //账号登录
		let result = await getLogin(params)
		console.log(result);
		console.log(result.token);
		if (result.status == 200) {
			localStorage.setItem("token", JSON.stringify(result.token))
			context.commit("changeIsLogin", result.token)
		}
	},
	// 手机号验证登陆
	async ActPhoneLogin(context, params) { //短信登录
		let result = await getPhoneLoginApi(params)
		console.log(result);
		if (result.status == 200) {
			localStorage.setItem("token", JSON.stringify(result.token))
			context.commit("changeIsLogin", result.data[0].username)
		}
	},
}
export default actions